﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Brain
{
    public class InputFreeCamera3D : FreeCamera3D
    {
        public float Speed = 1;
        public float GamepadSpeed = 5f;
        public Vector3 InitialRotationRadians;
        public Vector3 InitialPosition;

#if WINDOWS || XBOX
        public PlayerInfo Player;
#endif

        public float Sensitivity = 5f;

        public InputFreeCamera3D()
        {
        }
        public InputFreeCamera3D(GameScreen gs)
            : base(gs)
        {
        }
        protected VirtualThumbstick Movement;
        protected VirtualThumbstick Rotation;

        public InputFreeCamera3D(VirtualThumbstick movement, VirtualThumbstick rotation)
        {
            Movement = movement;
            Rotation = rotation;
        }
        public InputFreeCamera3D(VirtualThumbstick movement, VirtualThumbstick rotation, GameScreen gs)
            : this(movement, rotation)
        {
            gs.AddComponent(this);
        }

#if WINDOWS || XBOX
        public InputFreeCamera3D(PlayerInfo info, GameScreen gs)
            : base(gs)
        {
            this.Player = info;
        }
        public InputFreeCamera3D(PlayerInfo info)
        {
            this.Player = info;
        }
#endif
        public bool UpdateIfMouseOutOfScreen = false;

        public override void Draw()
        {
            IInputManager input = Engine.Instance.InputManager;

            GraphicsDevice device = Engine.Instance.GraphicsDevice;

            if (!UpdateIfMouseOutOfScreen)
            {
                Vector2 mousePos = input.GetMousePosition();
                int X = (int)mousePos.X;
                int Y = (int)mousePos.Y;
                if (X < 0 || Y < 0 ||
                    X > device.Viewport.Width ||
                    Y > device.Viewport.Height)
                {
                    return;
                }
            }

            Vector3 movementVector = Vector3.Zero;
            
#if WINDOWS || XBOX
            if (Player != null)
            {
                Vector2 leftThumstick = Engine.Instance.InputManager.GetLeftThumbstick(Player.PlayerIndex);
                movementVector += Vector3.Transform(new Vector3(leftThumstick.X, 0, -leftThumstick.Y) * GamepadSpeed, RotationMatrix);

                Vector2 rightThumstick = (Engine.Instance.InputManager.GetRightThumbstick(Player.PlayerIndex) / 50f) * Sensitivity;
                this.RotationRadians += new Vector3(-rightThumstick.X, rightThumstick.Y, 0);
            }
#endif
#if WINDOWS
            if (input.IsKeyDown(Keys.W))
            {
                movementVector += this.RotationMatrix.Forward;
            }
            if (input.IsKeyDown(Keys.S))
            {
                movementVector += this.RotationMatrix.Backward;
            }
            if (input.IsKeyDown(Keys.A))
            {
                movementVector += RotationMatrix.Left;
            }
            if (input.IsKeyDown(Keys.D))
            {
                movementVector += RotationMatrix.Right;
            }
            if (input.IsKeyDown(Keys.Q))
            {
                movementVector += RotationMatrix.Up;
            }
            if (input.IsKeyDown(Keys.Z))
            {
                movementVector += RotationMatrix.Down;
            }
#endif
#if WINDOWS_PHONE
            if (this.Movement != null)
            {
                Vector2 leftThumbstick = this.Movement.Difference;
                movementVector += Vector3.Transform(new Vector3(leftThumbstick.X, 0, -leftThumbstick.Y) * GamepadSpeed, RotationMatrix);
            }

            if (this.Rotation != null)
            {
                Vector2 rightThumstick = this.Rotation.Difference / 50f;
                this.RotationRadians += new Vector3(-rightThumstick.X, rightThumstick.Y, 0);
            }
#endif

            if (input.IsKeyDown(Keys.R))
            {
                this.Position = InitialPosition;
                this.RotationRadians = InitialRotationRadians;
            }
            this.Position += movementVector * Speed;

            if (input.IsMouseRightDown())
            {
                Vector2 dif = input.GetMouseDifference() / 50f;
                this.RotationRadians -= new Vector3(dif.X, dif.Y, 0);
            }

            base.Draw();
        }
    }
}
